package com.netbetter.langzheng.plugin.fileManager.reader;

import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.json.client.JSONException;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.netbetter.langzheng.commons.constants.AppEvents;
import com.netbetter.langzheng.plugin.fileManager.model.CommandResultModel;

/**
 * @author 嘉陵江里捉鱼 email:tang7259@163.com
 *@version 1.0
 *@time 2008-9-16 下午09:04:03
 */
public abstract class CommandRequestCallback implements RequestCallback {

    protected CommandResultModel result;

    public void onError(Request request, Throwable exception) {
        result = new CommandResultModel();
        result.setSuccess(false);
        result.setMessage(exception.toString());
    }

    public void onResponseReceived(Request request, Response response) {
        result = new CommandResultModel();
        String responseText = response.getText();
        try {
            JSONObject jsonValue = JSONParser.parse(responseText).isObject();
            result.setSuccess(jsonValue.get("success").isBoolean().booleanValue());
            result.setMessage(jsonValue.get("message").isString().stringValue());
            if (result.isSuccess()) {
                Dispatcher.forwardEvent(AppEvents.ChangeTreeItem);
            }
        } catch (JSONException e) {
            result.setSuccess(false);
            result.setMessage(e.getMessage());
        }

    }
}
